<?php

/*
	询单列表（工作人员）
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['tcp_status']=$this->tcp_status;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	//询单名称
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_msgorder_tcp',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$row){
		$row['workertitle']=pdo_getcolumn('jy_msgorder_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['workerid']),'title');
		$row['grouptitle']=pdo_getcolumn('jy_msgorder_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['groupid']),'title');
		
	}
	$return['groups']=pdo_getall('jy_msgorder_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'询单列表'),
		),
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'询单名称','type'=>'string'),
			array('name'=>'credit','title'=>'询单积分','type'=>'string'),
			array('type'=>'select','name'=>'groupid','title'=>'专家分类','options'=>'groups'),
			array('name'=>'workertitle','title'=>'专家','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'get_time','title'=>'领取时间','type'=>'time'),
			array('name'=>'complete_time','title'=>'完成时间','type'=>'time'),
			array('name'=>'status','title'=>'询单状态','type'=>'radio','options'=>'tcp_status'),
		),'action'=>array(
			array('type'=>'modal','op'=>'staff','title'=>'移交询单','show'=>array('status'=>array(1))),
			array('type'=>'href','op'=>'look','title'=>'查看询单'),
		)),
		'search'=>array(
			array('type'=>'select','width'=>15,'name'=>'groupid','title'=>'专家分类','options'=>'groups'),
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'询单状态','options'=>'tcp_status'),
			array('type'=>'string','width'=>15,'name'=>'title','title'=>'询单名称'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}

//提交询单
if($_RQ['op']=='complete'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'msg'=>trim($post['msg']),
			'status'=>2,
		);
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择询单！','error');
		}
		if(!$data['msg']){
			exi('请先填写询单处理结果！','error');
		}
		$post['is_worker']=1;
		$this->model('kefu')->addchatlog($post);
		exi('提交成功！','','list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择询单！','error');
	}
	$item=pdo_get('jy_msgorder_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if(!$item){
		exi('指定询单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'提交询单',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'询单名称','read'=>true),
			array('type'=>'string','name'=>'content','title'=>'询单内容','read'=>true),
			array('type'=>'text','name'=>'msg','title'=>'处理结果','help'=>'这里填写询单处理结果。做些说明即可。如果是开发型询单，直接填写”处理完毕“即可。'),

		),
	);
	exi($return);
}
if($_RQ['op']=='look'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'msg'=>trim($post['msg']),
			'status'=>2,
		);
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择询单！','error');
		}
		if(!$data['msg']){
			exi('请先填写询单处理结果！','error');
		}
		$post['is_worker']=1;
		$post['tcpid']=$post['id'];
		$this->model('kefu')->addchatlog($post);
		exi('提交成功！','','list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择询单！','error');
	}
	$item=pdo_get('jy_msgorder_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$log=pdo_getall('jy_msgorder_tcplog',array('uniacid'=>$_SESSION['uniacid'],'tcpid'=>$item['id']));
	foreach($log as &$lv){
		$lv['image']=json_decode($lv['image'],true);
		if(!$lv['is_worker']){
			$lv['isme']=1;
		}else{
			$lv['isme']=0;
		}
	}
	$item['logs']=$log;
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'询单详情',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
			array('type'=>'logs','name'=>'logs','jsname'=>'专家','title'=>'处理信息'),
			array('type'=>'text','name'=>'msg','title'=>'回复处理'),
		),
	);
	exi($return);
}
//指派询单
if($_RQ['op']=='staff'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$data=array(
			'workerid'=>$post['workerid'],
		);
		if(!$id){
			exi('请先选择询单！','error');
		}
		if(!$data['workerid']){
			exi('请先选择专家！','error');
		}
		pdo_update('jy_msgorder_tcp',$data,array('id'=>$id));
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择询单！','error');
	}
	$item=pdo_get('jy_msgorder_tcp',array('id'=>$id));
	if(!$item){
		exi('指定询单不存在！','error');
	}
	$return['workers']=pdo_getall('jy_msgorder_worker',array('uniacid'=>$_SESSION['uniacid']));
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'指派特定人员处理询单',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'询单名称','read'=>true),
			array('type'=>'select','name'=>'workerid','title'=>'指派专家','options'=>'workers'),
		),
	);
	exi($return);
}